package com.auxgroup.hr.front.site.co.process;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfPayHdrRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCAppStepInst;
import com.auxgroup.hr.front.dao.domain.PsCWfPayComp;
import com.auxgroup.hr.front.dao.domain.PsCWfPayHdr;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.process.ProcessService;
import com.auxgroup.hr.front.service.process.WfFileService;
import com.auxgroup.hr.front.service.process.WfPayHdrService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfPayHdrRespVO;

/**
 * 薪资发放审批
 * @author lv-PC
 *
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfPayHdr")
public class WfPayHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfPayHdrCO.class);

	@Autowired
	private WfPayHdrService wfPayHdrService;

	
	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	/**
	 * 获取薪资发放审批流程信息
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getPayInfo")
	public BaseResponseVO getOne(String cTransactionNbr,@SessionGain("user") UserDTO user) {
		logger.info("getWfDrftHdr param= {}", cTransactionNbr);
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		PsCWfPayHdr payHdr = wfPayHdrService.getWfPayHdr(cTransactionNbr);
		if(payHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		WfPayHdrRespVO respVo = objectMapper.map(payHdr, WfPayHdrRespVO.class);
		//获取薪资明细数据
		List<PsCWfPayComp> comps = wfPayHdrService.getPayComps(payHdr.getcTransactionNbr(), payHdr.getCalRunId());
		PsCWfPayComp allComp = getAllComp(comps);
		
		//设置公司列表数据
		respVo.setCompData(wfPayHdrService.compToCompList(comps));
		
		//设置月薪资数据
		respVo.setMonthData(wfPayHdrService.compAllToList(allComp));
		
		fillBaseWfInfo(user, payHdr, respVo);
		return buildRespone(respVo);
	}
	
	
	/**
	 * 获取Allcomp
	 * @param comps
	 * @return
	 */
	private PsCWfPayComp getAllComp(List<PsCWfPayComp> comps){
		PsCWfPayComp allComp = null;
		for (PsCWfPayComp comp : comps) {
			if("ALL".equals(comp.getcPayCompany())){
				allComp = comp;
			}
		}
		return allComp;
	}


	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
